import util.ListNode;

/**
 * Created by loso on 2017/3/18.
 */
public class _203_RemoveLinkedListElements {

    public static void main(String[] args) {
        _203_RemoveLinkedListElements a = new _203_RemoveLinkedListElements();
        a.removeElements(new ListNode(1),1);
    }

    public ListNode removeElements(ListNode head, int val) {
        if (head == null) {
            return null;
        }
        ListNode out = head;

        ListNode temp = head;
        while(head != null){
            if (head.val == val){
                out = head.next;
                head = head.next;
            }
            else{
                break;
            }
        }
        temp = head;
        if (head != null){
            return out;
        }
        while(head.next != null){
            head = head.next;
             if (head.val == val){
                 temp.next = head.next;
             }
             else{
                 temp = temp.next;
             }
        }
        if (head != null && head.val == val){
            temp.next = null;
        }
        return out;
    }


    //如果是一样的话就返回head.next,移除链表元素。
    public ListNode removeElementsBigBoss(ListNode head, int val) {
        if (head == null) return null;
        head.next = removeElementsBigBoss(head.next, val);
        return head.val == val ? head.next : head;
    }

}
